﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using DBModel;

namespace CV_Projekt
{
    public partial class FormHeadHunter : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var user = Session["User"] as HeadHunter;

            if (user == null)
            {
                Response.Redirect("Default.aspx");
            }
        }

        protected void ButtonSearchCV_Click(object sender, EventArgs e)
        {
            string searchWord = TextBoxFreeText.Text;
            List<CurriculumVitae> foundCvsList = CurriculumVitaeDB.GetCurriculumVitaesByFreeTextSearchWords(searchWord);
            ListViewCVSearchResult.DataSource = foundCvsList;
            ListViewCVSearchResult.DataBind();
        }

        protected void ButtonShowSelectedCV_Click(object sender, EventArgs e)
        {
            List<CurriculumVitae> curriculumVitaes = CurriculumVitaeDB.GetAllCurriculumVitaes();
            var foundCurriculumVitaes = new List<CurriculumVitae>();
            foreach (ListViewDataItem item in ListViewCVSearchResult.Items)
            {
                var isChecked = item.FindControl("CheckBoxCvId") as HtmlInputCheckBox;
                if (isChecked != null && isChecked.Checked)
                {
                    int cvId = int.Parse(isChecked.Value);
                    var foundCV =
                        curriculumVitaes.FirstOrDefault(c => c.CVId == cvId);
                        
                    if (foundCV != null)
                    {
                        foundCurriculumVitaes.Add(foundCV);
                    }
                }
            }
            ListViewCVPresentation.DataSource = foundCurriculumVitaes;
            ListViewCVPresentation.DataBind();

            ListViewCVEducations.DataSource = foundCurriculumVitaes;
            ListViewCVEducations.DataBind();

            ListViewCVEmployments.DataSource = foundCurriculumVitaes;
            ListViewCVEmployments.DataBind();

            ListViewCVSkills.DataSource = foundCurriculumVitaes;
            ListViewCVSkills.DataBind();

            ListViewCVOtherQualifications.DataSource = foundCurriculumVitaes;
            ListViewCVOtherQualifications.DataBind();

            ListViewCVLanguages.DataSource = foundCurriculumVitaes;
            ListViewCVLanguages.DataBind();

        }


        protected void ButtonFreeTextSearch_Click(object sender, EventArgs e)
        {
            string searchWord = TextBoxFreeText.Text;
            List<CurriculumVitae> foundCvsList = CurriculumVitaeDB.GetCurriculumVitaesByFreeTextSearchWords(searchWord);
            ListViewCVSearchResult.DataSource = foundCvsList;
            ListViewCVSearchResult.DataBind();
            ListViewCVPresentation.DataSource = null;
            ListViewCVPresentation.DataBind();

            ListViewCVEducations.DataSource = null;
            ListViewCVEducations.DataBind();

            ListViewCVEmployments.DataSource = null;
            ListViewCVEmployments.DataBind();

            ListViewCVSkills.DataSource = null;
            ListViewCVSkills.DataBind();

            ListViewCVOtherQualifications.DataSource = null;
            ListViewCVOtherQualifications.DataBind();

            ListViewCVLanguages.DataSource = null;
            ListViewCVLanguages.DataBind();
        }

        protected void ButtonSkillSearch_Click(object sender, EventArgs e)
        {
            string skill = TextBoxSkillSearch.Text;
            List<CurriculumVitae> foundCvsList = CurriculumVitaeDB.GetSkillBySearchWord(skill);
            ListViewCVSearchResult.DataSource = foundCvsList;
            ListViewCVSearchResult.DataBind();
        }

        protected void ButtonLanguageSearch_Click(object sender, EventArgs e)
        {
            string language = TextBoxLanguageSearch.Text;
            List<CurriculumVitae> foundCvsList = CurriculumVitaeDB.GetLanguageBySearchWord(language);
            ListViewCVSearchResult.DataSource = foundCvsList;
            ListViewCVSearchResult.DataBind();
        }

    }
}